<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\home\validate\StudentCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\facade\Route;
use app\home\BaseController;

class Student extends BaseController
{
    //选课
    public function index()
    {
        $userid=get_login_student('id');
        // dump($userid);die;
        if (!$userid) {
            redirect('/home/student/st_login')->send();
            die;
        }else{
            $student=Db::name('WckcStudent')->find($userid);
        // dump($student);
        $user=get_login_student();
        $where=[
            'grade'=>$user['grade'],
            'year'=>date('Y')
        ];
        
        $course_list=Db::name('WckcCourse')->field('id,cname')->where($where)->select();
        View::assign('course_list',$course_list); 
        //dump($course_list);
            if(!empty($student)){
                 View::assign('student',$student);
                 return View();
            }
        }
       
    }
    //学生登录
    public function st_login()
    {
        $param = get_params();
        // dump($param);
        $where['status']=['eq'=>1];
        $order=['bj_code'=>'asc'];

        $bj_list=Db::name('WckcClass')->field('bj_name,bj_code')->order($order)->where($where)->select();
        if (!empty($bj_list)) {
                View::assign('bj_list', $bj_list);
            }
      return view();

    }
    //学生选课信息
    public function courseinfo(){
        $userid=get_login_student('id');
        
        if (!$userid) {
            redirect('/home/student/st_login')->send();
            die;
        }
        $student=Db::name('WckcStudent')->find($userid);
        View::assign('student', $student);
        return view();
        //dump($student);die;

    }
    //提交选课
    public function course_submit(){
        $param = get_params();
        $param['update_time'] = time();
        $param['zy_time'] = time();
        $res = Db::name('WckcStudent')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
                // Db::name('User')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
        if ($res !== false) {            
            to_assign(0, '选课成功');
        } else {
            to_assign(1, '操作失败');
        }

        
    }
    //提交登录
    public function login_submit()
    {
        $param = get_params();
        
        try {
            validate(StudentCheck::class)->scene('login')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return to_assign(1, $e->getError());
        }
        

        $user = Db::name('WckcStudent')->where(['name' => $param['st_name'],'bj_name'=>$param['bj_name']])->find();
      
        if (empty($user)) {
            return to_assign(1, '用户名或密码错误1');
        }
         
         $param['pwd'] = intval($param['password']);
         
        if ($param['pwd'] !== $user['code']) {
            return to_assign(1, '用户名或密码错误2'.$param['pwd'].$user['code']);
        }
        if ($user['status'] == -1) {
            return to_assign(1, '该学生禁止登录,请于班主任联系');
        }
        
        $data = [
            'last_login_time' => time(),
            'last_login_ip' => request()->ip(),
            'login_times' => $user['login_times'] + 1,
        ];

        Db::name('WckcStudent')->where(['id' => $user['id']])->update($data);

        $userInfo = [
            'id' => $user['id'],
            'st_name' => $user['name'],
            'st_code' => $user['code'],            
            'grade'=> $user['grade']
            
        ];
        $session_student = get_config('app.session_student');
        Session::set($session_student, $userInfo);
        $token = make_token();
        set_cache($token, $userInfo, 7200);
        $userInfo['token'] = $token;
        return to_assign(0, '登录成功', $userInfo);
    }

    //退出登录
    public function login_out()
    {
        $session_student = get_config('app.session_student');
        Session::delete($session_student);
        return to_assign(0, "退出成功");
    }

/*
选择班级学生
*/
    public function dochangebj(){
        
         if (request()->isAjax()) {
        $param = get_params();
        $where['bj_name']=$param['bj_name'];
        $storder=['code'=>'asc'];
        $st_list=Db::name('WckcStudent')->field('code,name as st_name')->order($storder)->where($where)->select();
         return $st_list;
      }

    }



}
